package com.yuan.zs.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.baidu.cloud.media.player.IMediaPlayer;
import com.baidu.cloud.videoplayer.widget.BDCloudVideoView;
import com.yuan.base.framework.BaseFrameworkActivity;
import com.yuan.zs.R;
import com.yuan.zs.ui.wonderful.SimpleMediaController;

import java.util.Timer;
import java.util.TimerTask;


public class VideoDetailActivity extends BaseFrameworkActivity {

	// --------------------------控件--------------------------
	private BDCloudVideoView bdCloudVideoView;
	private SimpleMediaController simpleMediaController;
	private RelativeLayout rl_holder;

	// --------------------------变量--------------------------

	private Timer barTimer;
	private String url;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_video_detail;
	}

	@Override
	protected void bindViews() {
		rl_holder = findViewById(R.id.rl_holder);
		simpleMediaController = findViewById(R.id.media_controller_bar);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		url = "http://pgc.cdn.xiaodutv.com/1985141304_3243277022_20170520122351.mp4?authorization=bce-auth-v1%2Fc308a72e7b874edd9115e4614e1d62f6%2F2017-05-20T04%3A23%3A57Z%2F-1%2F%2Fb0545e034748711673218bca01da4d275db61160dd98e5f34b5d9698d59c138d&responseCacheControl=max-age%3D8640000&responseExpires=Mon%2C+28+Aug+2017+12%3A23%3A57+GMT&xcode=af895216f03ac3c9ef5deaa9e50278bede4581b471023180&time=1547824917&_=1547738519267";
		if (TextUtils.isEmpty(url)) {
			showToast("播放链接错误");
			return;
		}

		bdCloudVideoView = new BDCloudVideoView(this);
		bdCloudVideoView.setVideoPath(url);
		bdCloudVideoView.setVideoScalingMode(BDCloudVideoView.VIDEO_SCALING_MODE_SCALE_TO_FIT);

		RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(-1, -1);
		rllp.addRule(RelativeLayout.CENTER_IN_PARENT);
		rl_holder.addView(bdCloudVideoView, rllp);

		/*
		  注册listener
		 */
		bdCloudVideoView.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
			@Override
			public void onPrepared(IMediaPlayer iMediaPlayer) {

			}
		});
		bdCloudVideoView.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
			@Override
			public void onCompletion(IMediaPlayer iMediaPlayer) {

			}
		});
		bdCloudVideoView.setOnErrorListener(new IMediaPlayer.OnErrorListener() {
			@Override
			public boolean onError(IMediaPlayer iMediaPlayer, int i, int i1) {
				return false;
			}
		});
		bdCloudVideoView.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
			@Override
			public boolean onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
				return false;
			}
		});
		bdCloudVideoView.setOnBufferingUpdateListener(new IMediaPlayer.OnBufferingUpdateListener() {
			@Override
			public void onBufferingUpdate(IMediaPlayer iMediaPlayer, int percent) {
				if (simpleMediaController != null && bdCloudVideoView != null) {
					simpleMediaController.onTotalCacheUpdate(percent * bdCloudVideoView.getDuration() / 100);
				}
			}
		});
		bdCloudVideoView.setOnPlayerStateListener(new BDCloudVideoView.OnPlayerStateListener() {
			@Override
			public void onPlayerStateChanged(BDCloudVideoView.PlayerState nowState) {
				if (simpleMediaController != null) {
					simpleMediaController.changeState();
				}
			}
		});
		simpleMediaController.setMediaPlayerControl(bdCloudVideoView);
		bdCloudVideoView.start();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		if (bdCloudVideoView != null) {
			bdCloudVideoView.enterForeground();
		}
	}

	@Override
	protected void onStop() {
		if (bdCloudVideoView != null) {
			bdCloudVideoView.enterBackground();
		}
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (bdCloudVideoView != null) {
			bdCloudVideoView.stopPlayback();
		}
	}

	/**
	 * 检测'点击'空白区的事件，若播放控制控件未显示，设置为显示，否则隐藏。
	 */
	public void onClickEmptyArea(View v) {
		if (barTimer != null) {
			barTimer.cancel();
			barTimer = null;
		}
		if (this.simpleMediaController != null) {
			if (simpleMediaController.getVisibility() == View.VISIBLE) {
				simpleMediaController.hide();
			} else {
				simpleMediaController.show();
				hideOuterAfterFiveSeconds();
			}
		}
	}

	private void hideOuterAfterFiveSeconds() {
		if (barTimer != null) {
			barTimer.cancel();
			barTimer = null;
		}
		barTimer = new Timer();
		barTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				if (simpleMediaController != null) {
					simpleMediaController.getMainThreadHandler().post(() -> simpleMediaController.hide());
				}
			}

		}, 5 * 1000);

	}

}

